﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SynoManager.AudioStation.Data
{
    public class Playlist
    {
        internal readonly AudioStationService service;
        private readonly AudioStationClient.PlaylistInfo info;

        internal Playlist(AudioStationService service, AudioStationClient.PlaylistInfo info)
        {
            this.service = service;
            this.info = info;
        }

        internal Playlist(AudioStationService service, AudioStationClient.PlaylistResultData data)
        {
            this.service = service;
            info = new AudioStationClient.PlaylistInfo();
            info.id = data.index;
            info.text = info.qtip = data.title;
        }

        public int ID
        {
            get { return Int32.Parse(info.id); }
        }

        public string Name
        {
            get { return info.text; }
            set
            {
                var result = service.client.Rename(info.id, value);
                info.text = info.qtip = value;
            }
        }

        public string TooltipText
        {
            get { return info.qtip; }
        }

        public MusicFile[] GetFiles()
        {
            int total;
            return GetFiles(0, 0, out total);
        }

        public MusicFile[] GetFiles(int offset, int count, out int total)
        {
            List<MusicFile> result = new List<MusicFile>();
            AudioStationClient.PlaylistContents contents = service.client.GetPlaylistContents(info.id, offset, count);
            foreach (AudioStationClient.PlaylistItem file in contents.items)
            {
                result.Add(new MusicFile(this, file));
            }
            total = contents.total;
            return result.ToArray();
        }

        public void Delete()
        {
            service.client.Delete(info.id);
        }

        public void RemoveSongs(int[] songIds)
        {
            var result = service.client.RemoveSongs(info.id, songIds);
        }

        public void AddSongs(int[] songIds)
        {
            var result = service.client.AddSongs(info.id, songIds);
        }
    }
}
